package com.shuimin.service.service.sys;

import com.shuimin.service.AccessDeniedException;
import com.shuimin.service.App;
import com.shuimin.service.model.sys.Shop;
import pond.common.S;
import pond.common.struc.Cache;
import pond.db.JDBCTmpl;
import pond.db.sql.Criterion;
import pond.db.sql.Sql;
import pond.db.sql.SqlSelect;
import pond.security.rbac.RBAC;

import java.util.List;

/**
 * Created by sky on 15/12/24.
 */
public class ShopService {

  public static List<Shop> all() {
    RBAC.Roles roles = App.currentRoles();
    if (roles.hasAny(SysRoles.Roles.QUERY_ACROSS_SHOPS.id)) {
      SqlSelect sql =
              Sql.select("*").from(Shop.class).orderBy("seq");
      if (roles.hasNone(SysRoles.Roles.SHOP_MGMT.id)) {
        //管理门店是0，不显示门店为-1,其他门店为正数,必须拥有管理门店权限才能看到所有
        sql.where("seq", Criterion.GREATER_THAN, "0");
      }
      return App.DB.get(t -> t.query(Shop.class, sql));
    } else {
      Shop curShop = App.currentUser().shop();
      List<Shop> views = getByUser(App.currentUser().id());
      if (S._for(views).filter(v -> v.id().equals(curShop.id())).toList().size() < 1)
        views.add(curShop);
      return views;
    }
  }

  public static List<Shop> allForNormal() {
    RBAC.Roles roles = App.currentRoles();
      SqlSelect sql =
              Sql.select("*").from(Shop.class).orderBy("seq");
//        sql.where("seq", Criterion.GREATER_THAN, "0");
      return App.DB.get(t -> t.query(Shop.class, sql));
  }


  /**
   * @param user_id user's id is not uid
   * @return
   */
  public static List<Shop> getByUser(String user_id) {

    SqlSelect sqlSelect = Sql.select("*")
        .from(Shop.class)
        .join("r_user_view_shop")
        .on("t_shop.name = r_user_view_shop.shop")
        .where("r_user_view_shop.user", Criterion.EQ, user_id);

    return App.DB.get(t -> t.query(Shop.class, sqlSelect));
  }

  /**
   * 模糊查询
   *
   * @return
   */
  public static List<Shop> getByName(String name) {

    if (!App.currentRoles().hasAny(SysRoles.Roles.MANAGE_ACROSS_SHOPS.id)) {
      return S.array(App.currentUser().shop());
    }
    return App.DB.get(t -> t.query(Shop.class, "SELECT * FROM t_shop WHERE name like '%" + name + "%'"));
  }

  private static Cache<String, Shop> cache = Cache.lruCache(1000);

  public static Shop get(String id) {
    return cache.get(id, c -> {
      return App.DB.get(t -> t.recordById(Shop.class, id));
    });
  }

  public static void addShop(Shop shop, JDBCTmpl t) {
    t.recordInsert(shop);
    if (App.currentRoles().hasAny(SysRoles.Roles.SHOP_MGMT.id))
      cache.put(shop.id(), shop);
    else throw new AccessDeniedException(SysRoles.Roles.SHOP_MGMT);
  }

  public static void delShop(Shop shop, JDBCTmpl t) {
    t.recordDelete(shop);
    if (App.currentRoles().hasAny(SysRoles.Roles.SHOP_MGMT.id))
      cache.remove(shop.id());
    else throw new AccessDeniedException(SysRoles.Roles.SHOP_MGMT);
  }

  public static void updShop(Shop shop, JDBCTmpl t) {
    if (App.currentRoles().hasAny(SysRoles.Roles.SHOP_MGMT.id))
      if (t.recordExists(Shop.class, shop.id())) {
        t.recordUpdate(shop);
        cache.put(shop.id(), shop);
      } else throw new RuntimeException("" + shop + " does not exist");
    else throw new AccessDeniedException(SysRoles.Roles.SHOP_MGMT);
  }

  public static void addBrand() {

  }

}
